package cn.xd.base.domain.entity;

import cn.hutool.json.JSONUtil;
import cn.xd.base.basic.enums.BuriedPointStatusEnum;
import cn.xd.base.basic.type.*;
import cn.xd.core.ddd.entity.Entity;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class BuriedPoint implements Entity<BuriedPointId> {
    private BuriedPointId id;
    private BuriedPointPath methodPath;
    private BuriedPointParams params;
    private Remark remark;
    private BuriedPointStatus status;
    private BuriedPointResultInfo resultInfo;
    private UserId creatorId;

    private BuriedPointUserType creatorUserType;
    private TimeValue createTime;

    public static BuriedPoint error(String methodPath, String params, String remark, Long userId, Integer userType, Throwable throwable) {
        return BuriedPoint.builder().methodPath(BuriedPointPath.of(methodPath)).params(BuriedPointParams.of(params))
                .remark(Remark.of(remark)).creatorId(UserId.of(userId)).creatorUserType(BuriedPointUserType.of(userType))
                .createTime(TimeValue.now()).status(BuriedPointStatus.of(BuriedPointStatusEnum.ERROR.getCode()))
                .resultInfo(BuriedPointResultInfo.of(throwable.getMessage()))
                .build();
    }


    public static BuriedPoint success(String methodPath, String params, String remark, Long userId, Integer userType, Object object) {
        return BuriedPoint.builder().methodPath(BuriedPointPath.of(methodPath)).params(BuriedPointParams.of(params))
                .remark(Remark.of(remark)).creatorId(UserId.of(userId)).creatorUserType(BuriedPointUserType.of(userType))
                .createTime(TimeValue.now()).status(BuriedPointStatus.of(BuriedPointStatusEnum.ERROR.getCode()))
                .resultInfo(BuriedPointResultInfo.of(JSONUtil.toJsonStr(object)))
                .build();
    }
}
